﻿using Shop.Collect;
using Shop.LocalEvent;
using Shop.Model.DB;
using Shop.Model.SpecGroupT;
using Shop.RemoteModel.SpecGroupTemplate.Model;
using Shop.RemoteModel.SpecTemplate.Model;
using Shop.Service.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Shop.Service.lmpl
{
    internal class SpecGroupTemplateService : ISpecGroupTemplateService
    {
        private readonly ISpecGroupTemplateCollect _Service;
        private readonly ISpecsTemplateCollect _Spec;
        public SpecGroupTemplateService (ISpecGroupTemplateCollect service, ISpecsTemplateCollect specs)
        {
            this._Spec = specs;
            this._Service = service;
        }
        public SpecGroupTItem[] GetItems (long categoryId)
        {
            SpecGroupTName[] glist = this._Service.Gets<SpecGroupTName>(categoryId);
            if (glist.IsNull())
            {
                return Array.Empty<SpecGroupTItem>();
            }
            Dictionary<long, SpecTemplateItem[]> specs = this._Spec.GetSpecs(glist.ConvertAll(a => a.Id));
            return glist.ConvertAll(a => new SpecGroupTItem
            {
                Id = a.Id,
                GroupName = a.GroupName,
                Spec = specs.GetValueOrDefault(a.Id)
            });
        }
        public bool Set (long id, SpecGroupTSet set)
        {
            DBSpecGroupTemplate source = this._Service.Get(id);
            return this._Service.Set(source, set);
        }
        public void Delete (long id)
        {
            DBSpecGroupTemplate source = this._Service.Get(id);
            this._Service.Delete(source);
            new SpecGroupTemplateEvent("Delete", source).AsyncPublic();
        }
        public long Add (SpecGroupTSet add)
        {
            return this._Service.Add(add);
        }
        public SpecGroupTDto[] Gets (long categoryId)
        {
            SpecGroupT[] list = this._Service.Gets<SpecGroupT>(categoryId);
            if (list.IsNull())
            {
                return Array.Empty<SpecGroupTDto>();
            }
            SpecGroupTDto[] dtos = list.ConvertMap<SpecGroupT, SpecGroupTDto>();
            Dictionary<long, string[]> specs = this._Spec.GetSpecName(dtos.ConvertAll(a => a.Id));
            if (specs.Count > 0)
            {
                dtos.ForEach(c =>
                {
                    if (specs.TryGetValue(c.Id, out string[] spec))
                    {
                        c.Spec = spec;
                    }
                });
            }
            return dtos;
        }
        public Dictionary<long, int> SetSort (long id, int sort)
        {
            DBSpecGroupTemplate source = this._Service.Get(id);
            return this._Service.SetSort(source, sort);
        }
        public SpecGroupT Get (long id)
        {
            return this._Service.Get<SpecGroupT>(id);
        }
        public void SetIsEnable (long id, bool isEnable)
        {
            if (isEnable)
            {
                this._Spec.CheckIsNull(id);
            }
            DBSpecGroupTemplate source = this._Service.Get(id);
            this._Service.SetIsEnable(source, isEnable);
        }
    }
}
